import 'package:flutter/material.dart';
// import 'package:hkzf/pages/home/tab_search/filter_bar/data.dart';
import 'package:hkzf/scpoed_model/room_filter.dart';
import 'package:hkzf/widgets/common_title.dart';
import 'package:scoped_model/scoped_model.dart';

import 'data.dart';

class FilterDarwer extends StatelessWidget {
  const FilterDarwer({
    Key key,
  }) : super(key: key);
  @override
  Widget build(BuildContext context) {
    var dataList =
        ScopedModel.of<FilterBarModel>(context, rebuildOnChange: true).dataList;
    print('drawerbuild dataList');
    print(dataList);
    roomTypeList = dataList['roomTypeList'];
    orientedList = dataList['orientedList'];
    floorList = dataList['floorList'];
    print('dataList');
    print(dataList);
    List<String> selectedIds =
        ScopedModel.of<FilterBarModel>(context, rebuildOnChange: true)
            .selectedList
            .toList();

    _onchange(String val) {
      ScopedModel.of<FilterBarModel>(context, rebuildOnChange: true)
          .selectedListToggleItem(val);
    }

    return Drawer(
      child: SafeArea(
        child: ListView(
          children: [
            CommonTitle('户型'),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 4),
              child: FilterDrawerItem(
                list: roomTypeList,
                selectedIds: selectedIds,
                onChange: _onchange,
              ),
            ),
            CommonTitle('朝向'),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 4),
              child: FilterDrawerItem(
                list: orientedList,
                selectedIds: selectedIds,
                onChange: _onchange,
              ),
            ),
            CommonTitle('楼层'),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 4),
              child: FilterDrawerItem(
                list: floorList,
                selectedIds: selectedIds,
                onChange: _onchange,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class FilterDrawerItem extends StatelessWidget {
  final List<GeneralType> list;
  final List<String> selectedIds;
  final ValueChanged<String> onChange;
  const FilterDrawerItem({
    Key key,
    this.list,
    this.selectedIds,
    this.onChange,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Wrap(
      spacing: 10,
      runSpacing: 5,
      children: list == null
          ? []
          : list.map((e) {
              bool isActive = selectedIds.contains(e.id);
              return GestureDetector(
                  behavior: HitTestBehavior.translucent,
                  onTap: () {
                    onChange(e.id);
                  },
                  child: Container(
                      padding: EdgeInsets.all(10),
                      decoration: BoxDecoration(
                          color: isActive ? Colors.green : Colors.white,
                          borderRadius: BorderRadius.circular(5),
                          border: Border.all(
                            width: 1,
                            color: isActive ? Colors.white : Colors.green,
                          )),
                      child: Text(e.name)));
            }).toList(),
    );
  }
}
